package c.x.jy.common.utils;

import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import c.x.jy.common.rabbitMQ.RabbitMqConstant;

public class DeviceUtil {
	
	private final static String url="http://%s/mq/device/service?commandType=openOne&lockAddress=%s&lockTotalNum=1&uqKey=%s&deviceId=%s&sourceType=%s";
	
	private final static String msgUrl="http://%s/mq/device/screen/sendInfo?deviceId=%s&uqKey=%s&jsonData=%s";
	
	private final static String statusUrl="http://%s/mq/device/getBoxStatus?deviceId=%s&uqKey=%s&lockTotalNum=%s&lockAddress=%s";
	/**
	 *   客户存  开锁  G     店面取  S   通用  O 
	 * @param deviceImei  设备ID
	 * @param num  柜门号
	 * @param channelNum   总路（门）数
	 * @param channelnumofpcb  单板门数 
	 * @return
	 */
	public static boolean openDoorByCustomer(String deviceImei, int num  ,int channelNum,int channelnumofpcb) {
	   try { 
		     String res=	HttpClientUtil.sendHttpsPost(String.format(url,RabbitMqConstant.MQHost, HexUtil.inttoHexString((num/(channelnumofpcb+1))+1)+HexUtil.inttoHexString((num%channelnumofpcb)==0?channelnumofpcb:num%channelnumofpcb) ,RabbitMqConstant.uqKey,deviceImei,"G"),new HashMap());
			  if(res!=null&&JSON.parseObject(res).getInteger("code")!=null&&JSON.parseObject(res).getInteger("code").equals(0)) {
				  return true;
			  } 		 
		} catch (Exception e) {
			e.printStackTrace();
		}
		   return false;
		}
	
	/**
	 *   店面取 开锁  S      
	 * @param deviceImei  设备ID
	 * @param num  柜门号
	 * @param channelNum   总路（门）数
	 * @param channelnumofpcb  单板门数 
	 * @return
	 */
	public static boolean openDooryStore(String deviceImei, int num  ,int channelNum,int channelnumofpcb) {
	   try { 
		     String res=	HttpClientUtil.sendHttpsPost(String.format(url,RabbitMqConstant.MQHost, HexUtil.inttoHexString((num/(channelnumofpcb+1))+1)+HexUtil.inttoHexString((num%channelnumofpcb)==0?channelnumofpcb:num%channelnumofpcb) ,RabbitMqConstant.uqKey,deviceImei,"S"),new HashMap());
			  if(res!=null&&JSON.parseObject(res).getInteger("code")!=null&&JSON.parseObject(res).getInteger("code").equals(0)) {
				  return true;
			  } 		 
		} catch (Exception e) {
			e.printStackTrace();
		}
		   return false;
		}
	
	
	/**
	 *       通用开锁  O
	 * @param deviceImei  设备ID
	 * @param num  柜门号
	 * @param channelNum   总路（门）数
	 * @param channelnumofpcb  单板门数 
	 * @return
	 */
	public static boolean openDoor(String deviceImei, int num  ,int channelNum,int channelnumofpcb) {
	   try { 
		   System.out.println(channelnumofpcb);
		     String res=	HttpClientUtil.sendHttpsPost(String.format(url,RabbitMqConstant.MQHost, HexUtil.inttoHexString((num/(channelnumofpcb+1))+1)+HexUtil.inttoHexString((num%channelnumofpcb)==0?channelnumofpcb:num%channelnumofpcb) ,RabbitMqConstant.uqKey,deviceImei,"O"),new HashMap());
			System.out.println(res);
		     if(res!=null&&JSON.parseObject(res).getInteger("code")!=null&&JSON.parseObject(res).getInteger("code").equals(0)) {
				  return true;
			  } 		 
		} catch (Exception e) {
			e.printStackTrace();
		}
		   return false;
		}
	
		/**
		 * 无效
		 * @param deviceImei
		 * @param msg
		 * @return
		 */
	public static boolean  showMsg( String deviceImei ,String msg) {
		String res=	HttpClientUtil.sendHttpsPost(String.format(msgUrl,RabbitMqConstant.MQHost,deviceImei,  RabbitMqConstant.uqKey,msg),new HashMap());
		  if(res!=null&&JSON.parseObject(res).getInteger("code")!=null&&JSON.parseObject(res).getInteger("code").equals(0)) {
			  return true;
		  }else {
			  return false;
		  }
	}
	/**
	 * 异步刷新状态
	 * @param deviceImei
	 * @param num
	 * @param channelNum
	 * @param channelnumofpcb
	 * @return
	 */
	public static boolean  BoxStatus(String deviceImei, int num  ,int channelNum,int channelnumofpcb) {
		System.out.println("查询设备状态！");
		
		String res=	HttpClientUtil.sendHttpsPost(String.format(statusUrl,RabbitMqConstant.MQHost,  deviceImei,RabbitMqConstant.uqKey,1,HexUtil.inttoHexString((num/(channelnumofpcb+1))+1)+HexUtil.inttoHexString((num%channelnumofpcb)==0?channelnumofpcb:num%channelnumofpcb) ),new HashMap());
		
		if(res!=null&&JSON.parseObject(res).getInteger("code")!=null&&JSON.parseObject(res).getInteger("code").equals(0)) {
			  return true;
		  }else {
			  return false;
		  }
	}
	

	/**
	 * 取设备的十六进制地址
	 * @param num
	 * @param channelNum
	 * @param channelnumofpcb
	 * @return
	 */
	public static String getHexStringByDevice(int num  ,int channelNum,int channelnumofpcb) {
		return HexUtil.inttoHexString((num/(channelnumofpcb+1))+1)+HexUtil.inttoHexString((num%channelnumofpcb)==0?channelnumofpcb:num%channelnumofpcb);
	}

	public static void main(String[] args) throws  Exception {
		//System.out.println((36/(24+1))+1);
		String deviceNo="2000011388";
  // openDoorByCustomer(deviceNo ,1, 36, 24); //"{/'opty/':/'info/',/'addr/':/'xxxx/'}"	
 BoxStatus(deviceNo ,27, 36, 24); 
		    JSONObject o=new JSONObject();
		o.put("opty", "info"); 
		o.put("qr", "123456");
		o.put("phone", "123456");
 // showMsg(deviceNo, URLEncoder.encode(o.toJSONString(),"UTF-8") );
	 
	}
	
}
